package fun.lifepoem.store.service;

import fun.lifepoem.core.constant.Constants;
import fun.lifepoem.core.utils.DateUtils;
import fun.lifepoem.store.domain.LpShareRecord;
import fun.lifepoem.store.mapper.LpShareRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Yiwyn
 * @create 2023/3/11 15:16
 */
@Service
@Slf4j
public class ShareService {

    @Resource
    private LpShareRecordMapper lpShareRecordMapper;


    public void expiryFile() {
        List<LpShareRecord> lpShareRecords = lpShareRecordMapper.selectByExpiryStatus(Constants.INVALID);

        List<LpShareRecord> expiredShareList = lpShareRecords.stream().
                filter(p -> DateUtils.expried(p.getExpiryDt())).collect(Collectors.toList());

        List<Integer> ids = expiredShareList.stream().map(LpShareRecord::getId).collect(Collectors.toList());

        lpShareRecordMapper.batchUpdateToExpiry(ids);
    }


    public void expiryFile(LpShareRecord record) {
        record.setExpiryStatus(true);
        log.info("过期分享：{}", record);
        lpShareRecordMapper.updateByPrimaryKey(record);
    }

}
